package com.sp.scmdb.serviceImpl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.bean.WorkTypeBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.WorkTypeDao;
import com.sp.scmdb.entity.WorkType;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.service.WorkTypeService;

@Service("workTypeService")
public class WorkTypeServiceImpl implements WorkTypeService {

	private WorkTypeDao workTypeDao;
	private LogService logs;

	public void setLogs(LogService logs) {
		this.logs = logs;
	}
	
	public void setWorkTypeDao(WorkTypeDao workTypeDao) {
		this.workTypeDao = workTypeDao;
	}

	@Transactional(readOnly = true)
	public List<WorkType> queryAll() {
		return workTypeDao.queryAll();
	}

	@Transactional
	public String newWorkType(WorkTypeBean t, UserBean user) {
		String result="3";
		StringBuffer Hql = new StringBuffer("from WorkType where 1=1 ");
		if (StringUtils.isNotEmpty(t.getName())) {
			Hql.append("and name='").append(t.getName()).append("'");
			List<WorkType> list = workTypeDao.queryHql(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				WorkType workType = new WorkType();
				workType.setName(t.getName());
				workTypeDao.save(workType);
				logs.addLog(user, "添加 工种：\""+t.getName()+"\"成功！");	
				result = CommonParamter.CHECK_SUCCESS;
			}
			else{
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String editWorkType(WorkTypeBean bean, UserBean user) {
		String result = "2";
		// 检查角色名称是否重复
		if (StringUtils.isNotEmpty(bean.getName())) {
			StringBuffer Hql = new StringBuffer();
			Hql.append("from WorkType where id != " + bean.getId()
					+ " and name = '" + bean.getName() + "'");
			if (CollectionUtils.isEmpty(workTypeDao.queryHql(Hql.toString()))) {
				WorkType workType = workTypeDao.getById(Integer.parseInt(bean.getId()));
				// 如果名称没有重复的
				workType.setName(bean.getName());
				// 保存该工种
				workTypeDao.update(workType);
				logs.addLog(user, "添加 工种：\""+bean.getName()+"\"成功！");	
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String delWorkType(String[] ids, UserBean user){
		String result="1";
		WorkType workType = null;
		for (int i = 0; i < ids.length; i++) {
			workType = workTypeDao.getById(Integer.parseInt(ids[i]));
			if (workType != null) {
				logs.addLog(user, "添加 工种：\""+workType.getName()+"\"成功！");	
				// 删除工种信息
				workTypeDao.delete(workType);			
				
				result = CommonParamter.CHECK_SUCCESS;
			}
		}
		return result;
	}

	@Transactional(readOnly = true)
	public WorkType findID(String depId) {
		
		return workTypeDao.getById(Integer.parseInt(depId));
	}
}
